<template>
  <div>
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
        <a-button type="primary" @click="deleteBatch"> 删除 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              title: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <MobileApplistConfigListDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import MobileApplistConfigListDrawer from './mobileApplistConfigListDrawer.vue';
  import { columns, searchFormSchema } from './mobileApplistConfigList.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getAdminListMobileAppIcon, getAdminDelMobileAppIcon } from '/@/api/system/system';
  export default defineComponent({
    name: 'MobileApplistConfigList',
    components: { BasicTable, TableAction, MobileApplistConfigListDrawer },
    setup() {
      const { createMessage, createConfirm } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();

      const [registerTable, { reload, getSelectRowKeys }] = useTable({
        title: '手机端应用',
        api: getAdminListMobileAppIcon,
        columns,
        formConfig: {
          labelWidth: 80,
          schemas: searchFormSchema,
        },
        searchInfo: {
          op: 'search',
        }, //额外的参数
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        clickToRowSelect: false,
        rowSelection: {
          type: 'checkbox',
        },
        rowKey: 'id',
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await getAdminDelMobileAppIcon({ ids: record.id }).then(() => {
          handleSuccess();
        });
      }

      //批量删除
      const deleteBatch = async () => {
        let keys = getSelectRowKeys();
        if (keys.length == 0) {
          createMessage.warning('请选择需要删除的数据');
          return;
        }
        createConfirm({
          iconType: 'warning',
          title: '确定删除吗？',
          onOk: async () => {
            await getAdminDelMobileAppIcon({
              ids: keys.join(','),
            }).then(() => {
              handleSuccess();
            });
          },
        });
      };

      function handleSuccess() {
        reload();
      }
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        deleteBatch,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
      };
    },
  });
</script>
